﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SHNOPData.SHNOPModel;
using SHNOPData.common;

namespace SHNOPData.ImportExcel
{
    public class ImportRuwang : IImportExcel
    {
        public void Import(string[,] array)
        {
            string retMsg = "";
            string errorMsg = "";
            int successCount = 0;
            List<InNetInfo> listZd = GetInNetInfoList(array, out errorMsg);
            int failCount = array.GetLength(0) - listZd.Count-1;
            foreach (var item in listZd)
            {
                bool isValidId = IsValidPlanID(item, ref retMsg);
                if (!isValidId)
                {
                    failCount++;
                    continue;
                }
                bool result = AppDBMng.ImportInNet(item, ref retMsg);
                if (result)
                {
                    successCount += 1;
                }
                else
                {
                    failCount++;
                    retMsg = retMsg + "第" + item.No + "条记录导入失败\n";
                }
            }
            if (failCount == 0)
            {
                System.Web.HttpContext.Current.Session["batchImport"] = "成功导入" + successCount.ToString() + "条记录";
            }
            else
            {
                System.Web.HttpContext.Current.Session["batchImport"] = "成功导入" + successCount.ToString() + "条,失败" + failCount.ToString() + "条\n" + retMsg;
            }
           
        }
        private bool IsValidPlanID(InNetInfo item, ref string retMsg)
        {
            if (item.GuihuaID2G.Length > 0)
            {
                item.GuihuaIDTD = null;
                return true;
            }
            else if (item.GuihuaID2G.Length == 0 && item.GuihuaIDTD.Length == 0)
            {
                retMsg = retMsg + "第" + item.No + "条记录导入失败,规划编号必须至少填写一个\n";
            }
            return false;
        }
        private int? Convert2Int(string info)
        {
            int outResult = 0;
            int? valOrNull = (info.Length != 0 && int.TryParse(info, out outResult)) ? outResult : (int?)null;
            return valOrNull;
        }

        public List<InNetInfo> GetInNetInfoList(string[,] array, out string msg)
        {
            msg = "";
            List<InNetInfo> netList = new List<InNetInfo>();
            if (array != null)
            {
                for (int i = 1; i < array.GetLength(0); i++)
                {
                    int j = 0;
                    while (j < array.GetLength(1))
                    {
                        try
                        {
                            InNetInfo net = new InNetInfo();
                            #region 入网
                            net.No = array[i, j++];
                            if (net.No.Length == 36)
                            {
                                net.ruWangID = new Guid(net.No);
                            }
                            net.company = array[i, j++];
                            net.GuihuaID2G = array[i, j++];
                            net.GuihuaIDTD = array[i, j++];
                            net.RuWangQingKuang = "已入网";
                            net.RuWangDate = DateTime.Now.ToString();
                            net.GWangName = array[i, j++];
                            net.DWangName = array[i, j++];
                            net.isComplete = 1;
                            net.isRuWang = array[i, j++].Trim()=="是"?1:0;
                            net.RuWangReason=array[i, j++];
                            net._900ShenqingNum=Convert2Int(array[i, j++]);
                            net._1800ShenqingNum=Convert2Int(array[i, j++]);
                            net.totalPinNum=Convert2Int(array[i, j++]);
                            net._900DuanKouNum=Convert2Int(array[i, j++]);
                            net._1800DuanKouNum=Convert2Int(array[i, j++]);
                            net.totalDuankouNum=Convert2Int(array[i, j++]);
                            net.TWangName=array[i, j++];
                            net.TDPinNum=Convert2Int(array[i, j++]);
                            net.TDDuanKouNum=Convert2Int(array[i, j++]);
                            net.TDPartInNetEd=array[i, j++];
                            net.TDCompleteDate=array[i, j++];
                            #endregion
                            netList.Add(net);
                        }
                        catch (System.Exception ex)
                        {
                        	//msg
                        }
                        
                    }

                }
            }
            return netList;
        }
    }
}
